package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailAccountDTO;
import com.dodoke.dto.RetailStoreDTO;
import com.dodoke.entity.RetailAccount;
import com.dodoke.entity.RetailServiceCenter;
import com.dodoke.service.IServiceRetailAccount;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import com.dodoke.vo.RetailServiceCenterDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 人员/商户控制层接口
 */
@Slf4j
@RestController
@RequestMapping("/retail/account")
public class RetailAccountController {
    @Autowired
    private IServiceRetailAccount retailAccountService;

    /**
     * 人员/商户分页请求接口
     *
     * @param currentPage      当前页码
     * @param pageSize         每页显示数量
     * @param retailAccountDTO 模糊查询条件
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, RetailAccountDTO retailAccountDTO) {
        return AjaxResult.success("人员/商户列表请求成功", retailAccountService.getListByPage(currentPage, pageSize, retailAccountDTO));
    }

    /**
     * 新增人员/商户
     *
     * @param retailAccount 人员/商户信息参数
     * @return
     */
    @PostMapping
    public AjaxResult addRetailAccount(@Validated @RequestBody RetailAccount retailAccount) {
        // 判断人员/商户手机号是否重复
        RetailAccount retailAccountByPhone = retailAccountService.getOne(new LambdaQueryWrapper<RetailAccount>().eq(RetailAccount::getPhone, retailAccount.getPhone()));
        if (retailAccountByPhone != null) {
            return AjaxResult.error(HttpStatus.ERROR, "人员/商户手机号已存在");
        }
        int resCount = retailAccountService.saveRetailAccount(retailAccount);
        if (resCount <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "人员/商户添加失败");
        }
        return AjaxResult.success("新增人员/商户成功");
    }

    /**
     * 修改人员/商户
     *
     * @param retailAccount 人员/商户参数
     * @return
     */
    @PutMapping
    public AjaxResult editRetailAccount(@Validated @RequestBody RetailAccount retailAccount) {
        // 判断人员/商户手机号是否存在
        RetailAccount retailAccountByPhone = retailAccountService.getOne(new LambdaQueryWrapper<RetailAccount>().eq(RetailAccount::getPhone, retailAccount.getPhone()));
        if (retailAccountByPhone != null && !retailAccount.getId().equals(retailAccountByPhone.getId())) {
            return AjaxResult.error(HttpStatus.ERROR, "人员/商户手机号已存在");
        }
        int resCount = retailAccountService.editRetailAccount(retailAccount);
        if (resCount <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "人员/商户修改失败");
        }
        return AjaxResult.success("修改人员/商户成功");
    }

    /**
     * 根据id获取人员/商户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.success("获取人员/商户信息成功", retailAccountService.getById(id));
    }

    /**
     * 根据id删除人员/商户信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Long id) {
        boolean isRemove = retailAccountService.removeById(id);
        return isRemove ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    /**
     * 根据id设置负责人
     * @param id
     * @return
     */
    @GetMapping("/setMainPerson/{id}")
    public AjaxResult setMainPerson(@PathVariable Long id) {
        boolean resCount = retailAccountService.setIsMain(id);
        return resCount ? AjaxResult.success("设置成功") : AjaxResult.error("设置失败");
    }
}
